$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
})


function stopBubble(e) {
//如果提供了事件对象，则这是一个非IE浏览器
    if ( e && e.stopPropagation )
    //因此它支持W3C的stopPropagation()方法
        e.stopPropagation();
    else
    //否则，我们需要使用IE的方式来取消事件冒泡
        window.event.cancelBubble = true;
}

function showDiag(id) {
    var diagid = document.getElementById(id);
    var windowDiag = diagid.getElementsByClassName('windowDiag')[0];
    diagid.style.display = 'block';
    startMove(windowDiag,{marginTop:120},MOVE_TYPE.BUFFER);
}

function closeDiag(id) {
    var diagid = document.getElementById(id);
    var windowDiag = diagid.getElementsByClassName('windowDiag')[0];
    startMove(windowDiag,{marginTop:300},MOVE_TYPE.BUFFER);
    setTimeout(function () {
        diagid.style.display = 'none';
    },300);
}

function sexselect() {
    var edit_select_list = document.getElementsByClassName('edit_select_list')[0];
    var select_sex = document.getElementsByClassName('select_sex')[0];
    var select_sexinp = document.getElementById('sexinp');
    edit_select_list.style.display = 'block';
    var edit_select_listp = edit_select_list.getElementsByTagName('p');
    for (var i=0; i<edit_select_listp.length;i++){
          edit_select_listp[i].onclick = function (e) {
                var sid = this.getAttribute('sid');
                select_sex.innerHTML = this.innerHTML;
                select_sexinp.value = sid;
                edit_select_list.style.display = 'none';
                stopBubble(e);
          }

    }
}



/**
 * 提交个人资料
 */
function doSubmit() {
    var input = document.getElementsByClassName('profile_edit_inp')[0].getElementsByTagName('input');
    var text = document.getElementsByClassName('profile_edit_inp')[0].getElementsByTagName('textarea')[0];
    var qq = input[4].value;
    if (qq) {
        var reg = /^\d{5,10}$/;
        //用上面定义的正则表达式测试，如果不匹配则返回false
        if (!reg.test(qq)) {
            layer.msg('QQ号码不正确', {icon: 2});
            return false;
        }
    }
    // 验证
    var data = {};
    data.realname = input[0].value;
    data.sex      = input[1].value;
    data.interest = input[2].value;
    data.birthday = input[3].value;
    data.qq       = input[4].value;
    data.weixin   = input[5].value;
    data.sign     = text.value;
    $.ajax({
        type:"POST",
        url:"/profile/upload-memberbase-info",
        data:data,
        dataType:"json",
        success:function (res) {
            if (res['code']==200) {
                layer.msg('修改成功');
                closeDiag('edit_profile');
            }else{
                layer.msg('修改失败');
            }
        }
    })
}

function roomadminShow() {
    showDiag('room_admin');
    $.ajax({
        type:"get",
        url:'/profile/gaglist',
        data:{},
        dataType:"json",
        success:function (res) {
            var loading = document.getElementsByClassName('loading')[0];
            loading.style.display = 'none';
            if (res['code']==200){
                 var room_admin_list = document.getElementsByClassName('room_admin_list')[0].getElementsByTagName('ul')[0];

                 var _html = '';
                 for (var i=0;i<res['data'].length;i++){
                      _html += '<li>'+res['data'][i]['nickname']+' <span uid="'+res['data'][i]['uid']+'"  room="'+res['data'][i]['room']+'"  onclick="delgag(this)">取消</span></li>';
                 }
                 room_admin_list.innerHTML = _html;
            }
        }
    })
}

/**
 * 取消禁言
 */
function delgag(_this) {
    var uid = _this.getAttribute('uid');
    var room = _this.getAttribute('room');
    var data = {uid:uid,room:room};
    $.ajax({
        type:"POST",
        url:"/profile/del-gag",
        data:data,
        dataType:"JSON",
        success:function (res) {
            console.log(res);
        }
    })
}

function showWindialg(op){
    if (op=='admin') {
        showDiag('admin');
        $.ajax({
            type:"get",
            url:'/profile/roomadminlist',
            data:{},
            dataType:"json",
            success:function (res) {
                var loading = document.getElementById(op).getElementsByClassName('loading')[0];
                loading.style.display = 'none';
                if (res['code']==200){
                    var room_admin_list = document.getElementById(op).getElementsByClassName('room_admin_list')[0].getElementsByTagName('ul')[0];

                    var _html = '';
                    for (var i=0;i<res['data'].length;i++){
                        _html += '<li>'+res['data'][i]['member']['nickname']+' <span uid="'+res['data'][i]['uid']+'"  room="'+res['data'][i]['room']+'"  onclick="delgag(this)">下房管</span></li>';
                    }
                    room_admin_list.innerHTML = _html;
                }
            }
        })
    }
    if (op=='gag') {
        showDiag('gag');
        $.ajax({
            type:"get",
            url:'/profile/gaglist',
            data:{},
            dataType:"json",
            success:function (res) {
                var loading = document.getElementById(op).getElementsByClassName('loading')[0];
                loading.style.display = 'none';
                if (res['code']==200){
                    var room_admin_list = document.getElementById(op).getElementsByClassName('room_admin_list')[0].getElementsByTagName('ul')[0];

                    var _html = '';
                    for (var i=0;i<res['data'].length;i++){
                        _html += '<li>'+res['data'][i]['nickname']+' <span uid="'+res['data'][i]['uid']+'"  room="'+res['data'][i]['room']+'"  onclick="delgag(this)">取消</span></li>';
                    }
                    room_admin_list.innerHTML = _html;
                }
            }
        })
    }
    if (op=='fans') {
        showDiag('fans');
        $.ajax({
            type:"get",
            url:'/profile/fanspai',
            dataType:"json",
            success:function (res) {
                console.log(res);
                var fans_text  = document.getElementsByClassName('fans_text');
                var fans_status = document.getElementsByClassName('fs_status');
                for (var i=0;i<res.length;i++) {
                    if (res[i]['id']){
                        fans_status[i].className = 'fs_status opend';
                        fans_status[i].innerHTML = '已开通';
                        fans_text[i].innerHTML = res[i]['fans_brand_title'];
                    }
                }
            }
        })
    }
}

/**
 * 开通粉丝勋章
 */
function openfans(_this) {
    var lev = _this.getAttribute('data-lev');
    $.ajax({
        type:"POST",
        url:"/profile/checkopenfans",
        data:{lev:lev},
        dataType:"json",
        success:function (res) {
            if (res['code']==200){
                var fs_layout = document.getElementsByClassName('layer_fs')[0];
                var fs_button = fs_layout.getElementsByClassName('fs_c')[0];
                fs_button.setAttribute('lev',res['lev']);
                fs_layout.style.display = 'block';
            }else{
                layer.msg(res['msg'],{icon:2});
            }
        }
    })
}

function openfanssumit(_this) {
    var lev = _this.getAttribute('lev');
    var title = document.getElementById('fs_input').value;
    if (title.length!=3){
        layer.msg("请输入3个字符",{icon:2});
        return;
    }
    $.ajax({
        type:"POST",
        url:"/profile/addfans",
        data:{lev:lev,title:title},
        dataType:"json",
        success:function (res) {
              var fs_layout = document.getElementsByClassName('layer_fs')[0];
              fs_layout.style.display = 'none';
              if (res['code']==200){
                  closeDiag('fans');
                  var fs_layout = document.getElementsByClassName('layer_fs')[0];
                  fs_layout.style.display = 'none';
              }else{
                  layer.msg(res['msg'],{icon:1});
              }
        }
    })
}